package com.atguigu.ssyx.acl.controller;

import com.atguigu.ssyx.acl.service.AdminRoleService;
import com.atguigu.ssyx.acl.service.AdminService;
import com.atguigu.ssyx.acl.service.RoleService;
import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.common.utils.MD5;
import com.atguigu.ssyx.model.acl.Admin;
import com.atguigu.ssyx.vo.acl.AdminQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author 杨林
 * @create 2023-07-06 17:27 星期四
 * description:用户管理
 */
@RestController
@Slf4j
@RequestMapping("/admin/acl/user")
@Api(tags = "用户管理")
@CrossOrigin  //跨域
public class AdminController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private AdminRoleService adminRoleService;

    @Autowired
    private RoleService roleService;

    @ApiOperation(value = "获取用户分页列表")
    @GetMapping("/{page}/{limit}")
    public Result index(@PathVariable("page") Long page,
                        @PathVariable("limit") Long limit,
                        AdminQueryVo adminQueryVo){   //注意：这里接收参数时不能使用 @RequestParam
        Page<Admin> adminPage = new Page<>(page, limit);
        IPage<Admin> pageModel =  adminService.selectPage(adminQueryVo,adminPage);
        return Result.ok(pageModel);
    }

    @ApiOperation(value = "获取管理用户")
    @GetMapping("/get/{id}")
    public Result get(@PathVariable("id") Long id){
        Admin user = adminService.getById(id);
        return Result.ok(user);
    }

    @ApiOperation("新增管理用户")
    @PostMapping("/save")
    public Result save(@RequestBody Admin user){
        //对前端传来的密码进行MD5加密
        user.setPassword(MD5.encrypt(user.getPassword()));
        adminService.save(user);
        return Result.ok();
    }

    @ApiOperation(value = "修改管理用户")
    @PutMapping("/update")
    public Result updateById(@RequestBody Admin user){
        adminService.updateById(user);
        return Result.ok();
    }

    @ApiOperation(value = "删除管理用户")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable("id") Long id){
        adminService.removeById(id);   //逻辑删除
        return Result.ok();
    }

    @ApiOperation(value = "根据id列表删除管理用户")
    @DeleteMapping("/batchRemove")
    public Result batchRemove(@RequestBody List<Long> ids){
        adminService.removeByIds(ids);
        return Result.ok();
    }


    /**
     * 获取所有用户：
     *      用于用户选择角色
     *  获取当前用户的所有角色：
     *      用于回显当前用户的所有角色
     * @param adminId
     * @return
     */
    @ApiOperation(value = "获取所有的角色并根据用户id获取用户已经分配的角色")
    @GetMapping("/toAssign/{adminId}")
    public Result getRoleByAdminId(@PathVariable("adminId") Long adminId){
        //最终的返回结果包含两部分内容：所有角色 和 根据用户id获取到的已经分配的角色
        Map<String,Object> result = roleService.getRoleAndUserRole(adminId);
        return Result.ok(result);
    }


    @PostMapping("/doAssign")
    @ApiOperation(value = "给用户分配角色")
    public Result assignsRoles(@RequestParam("adminId") Long adminId,@RequestParam("roleId") List<Long> roleIds){
        adminRoleService.assignsRoles(adminId,roleIds);
        return Result.ok();
    }

}
